{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "_cell_guid": "79c7e3d0-c299-4dcb-8224-4455121ee9b0",
    "_kg_hide-output": false,
    "_uuid": "d629ff2d2480ee46fbb7e2d37f6b5fab8052498a",
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "!apt-get install --no-install-recommends -y openmpi-bin libopenmpi-dev libopencv-dev python-opencv && ln -sf /usr/lib/x86_64-linux-gnu/libmpi_cxx.so /usr/lib/x86_64-linux-gnu/libmpi_cxx.so.1 && ln -sf /usr/lib/x86_64-linux-gnu/openmpi/lib/libmpi.so /usr/lib/x86_64-linux-gnu/openmpi/lib/libmpi.so.12 && ln -sf /usr/lib/x86_64-linux-gnu/libmpi.so /usr/lib/x86_64-linux-gnu/libmpi.so.12 && pip install cntk"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/opt/conda/lib/python3.6/site-packages/cntk/cntk_py_init.py:56: UserWarning: Unsupported Linux distribution (sion_id=\"9\"-tps://www.debian.org/). CNTK supports Ubuntu 16.04 and above, only.\n",
      "  warnings.warn('Unsupported Linux distribution (%s-%s). CNTK supports Ubuntu 16.04 and above, only.' % (__my_distro__, __my_distro_ver__))\n",
      "/opt/conda/lib/python3.6/site-packages/cntk/cntk_py_init.py:98: UserWarning: \n",
      "\n",
      "################################################ Missing optional dependency (GPU-Specific) ################################################\n",
      "   CNTK may crash if the component that depends on those dependencies is loaded.\n",
      "   Visit https://docs.microsoft.com/en-us/cognitive-toolkit/Setup-Linux-Python#optional-gpu-specific-packages for more information.\n",
      "############################################################################################################################################\n",
      "If you intend to use CNTK without GPU support, you can ignore the (likely) GPU-specific warning!\n",
      "############################################################################################################################################\n",
      "\n",
      "  warnings.warn(WARNING_MSG_GPU_ONLY % ('GPU-Specific', 'https://docs.microsoft.com/en-us/cognitive-toolkit/Setup-Linux-Python#optional-gpu-specific-packages'))\n"
     ]
    }
   ],
   "source": [
    "import cntk as C\n",
    "\n",
    "model_path = 'checkpoint/model.cntk'\n",
    "\n",
    "ctnk_model = C.load_model(model_path)\n",
    "ctnk_model.save('checkpoint/model.onnx', format=C.ModelFormat.ONNX)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Collecting onnxruntime\n",
      "\u001b[?25l  Downloading https://files.pythonhosted.org/packages/3b/13/1ac0397bcffb30d03af3de80eae9b960883d2e43e3a562423260948390f5/onnxruntime-1.1.1-cp36-cp36m-manylinux1_x86_64.whl (3.6MB)\n",
      "\u001b[K     |████████████████████████████████| 3.6MB 2.8MB/s eta 0:00:01\n",
      "\u001b[?25hInstalling collected packages: onnxruntime\n",
      "Successfully installed onnxruntime-1.1.1\n"
     ]
    }
   ],
   "source": [
    "!pip install -q onnxruntime"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "import onnxruntime\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "ort_session = onnxruntime.InferenceSession(\"checkpoint/model.onnx\")\n",
    "\n",
    "x = np.random.rand(1, 3, 299, 299).astype(np.float32)\n",
    "# compute ONNX Runtime output prediction\n",
    "ort_inputs = {ort_session.get_inputs()[0].name: x}\n",
    "ort_outs = ort_session.run(None, ort_inputs)\n",
    "\n",
    "# compute the ctnk output\n",
    "cntk_out = ctnk_model.eval(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.testing.assert_allclose(np.array(cntk_out), ort_outs[0], rtol=1e-03, atol=1e-05)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
